<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card v-loading="isLoad" class="box-card" style="padding:100px 50px">
        <!-- 头部 -->
        <tree-tool :tree-node=" company" :show-dialog.sync="showAddDialogVisible" :is-root="true" />
        <!-- 树形结构 -->

        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <!-- 作用域插槽 data：treeData 相当于每一项数据 node 节点 -->
          <template slot-scope="{data}">
            <tree-tool :tree-node="data" @addDepts="addDepts" @deleteDepts="getDepartments" @showEdit="onEdit" />
          </template>
        </el-tree>

      </el-card>
    </div>
    <!-- 添加部门弹层 -->
    <AddDepts ref="addDepts" :show-dialog.sync="showAddDialogVisible" :current-node="currentNode" @addSuccess="getDepartments" />
  </div>

</template>

<script>
import treeTool from './components/tree-tool.vue'
import { getDepartments } from '@/api/departments'
import { transListToTree } from '@/utils'
import AddDepts from './components/add-depts.vue'

export default {
  components: { treeTool, AddDepts },
  data() {
    return {
      company: { }, // 就是头部的数据结构
      departs: [],
      defaultProps: {
        label: 'name'
      },
      showAddDialogVisible: false, // 添加部门弹层
      currentNode: {},
      isLoad: false
    }
  },

  created() {
    this.getDepartments() // 调用自身的方法
  },

  methods: {
    async getDepartments() {
      this.isLoad = true
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: '负责人' }
      this.departs = transListToTree(result.depts, '') // 需要将其转化成树形结构
      this.isLoad = false
      // console.log(result)
    },
    // 显示添加部门弹层
    addDepts(node) {
      this.showAddDialogVisible = true
      this.currentNode = node
      console.log(node)
    },
    // 点击显示编辑部门
    onEdit(val) {
      this.showAddDialogVisible = true
      this.currentNode = val
      this.$refs.addDepts.getDeptDetail(val.id)
    }
  }
}
</script>

<style scoped lang='less'>

</style>
